<?php
namespace Common\Controller;
use Think\Controller;
class OutputFileController extends Controller {

    //输出pdf文件的控制器

    protected $host = "http://localhost";
    protected $show_ctl = "/Home/Type16pf/show_16pf/";
    //protected $pdf_render ="D:\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
    protected $pdf_render ="/usr/local/bin/wkhtmltopdf";
   // protected $urlpara = "/display/1";


    public function outUserReport($uid, $quiz_id)
    {
        if($quiz_id == 6){
            $filename = "16pf_report_".$uid.".pdf";
            $url = "/Quiz/Pf16/show_16pf/uid/".$uid."?chartwidth=900";
            $render_arg = "--javascript-delay 200";
            $this->outPdf($url, $filename, $render_arg);
        }
        else if($quiz_id == 7){
            $filename = "wippf_report_".$uid.".pdf";
            $url = "/Quiz/Wippf/show_wippf/user_id/".$uid."?noline=1";
            $render_arg = "--javascript-delay 200 --print-media-type";
            $this->outPdf($url, $filename, $render_arg);
        }
        else if($quiz_id == 12){
            $filename = "mbti_report_".$uid.".pdf";
            $url = "/Quiz/Mbti/show_mbti/user_id/".$uid;
            $this->outPdf($url, $filename);
        }
        else if($quiz_id == 14){
            $filename = "disc_report_".$uid.".pdf";
            $url = "/Quiz/CustomQuiz/show_report/quiz_id/14/type/count/uid/".$uid;
            $this->outPdf($url, $filename);
        }
    }

    public function out16pfReport($uid)
    {
        $filename = "16pf_report_".$uid.".pdf";
        $show_ctl = "/Quiz/Type16pf/show_16pf/";
        $ctlstr = $show_ctl."uid/".$uid;
        $this->outPdf($ctlstr, $filename);
    }

    public function out16pfGroupReport($oid = 0, $quiztitle = ""){
        $show_ctl = "/Home/Type16pf/show_16pf_groupreport/";
        if (!$oid && !$quiztitle){
            $ctlstr = $show_ctl;
            $filename = "16pf_report_singlegroup.pdf";
        }
        else{
            $ctlstr = $show_ctl."oid/".$oid."/quiztitle/".urlencode($quiztitle);
            $filename = "16pf_report_".urlencode($quiztitle).".pdf";
        }

        header("Content-type: text/html; charset=utf-8");
        //var_dump($ctlstr);
        $this->outPdf($ctlstr, $filename);
    }



    public function outPdf($ctlstr, $filename, $render_arg = ""){
        $savepath = $_SERVER['DOCUMENT_ROOT'].'/userfiles/';

        $cmd = $this->pdf_render." ".$render_arg." ".$this->host.$ctlstr." ".$savepath.$filename;
        //var_dump($cmd);

        $return  = system($cmd, $out);

        if (!file_exists($savepath.$filename)) { //检查文件是否存在

            var_dump($cmd);
            var_dump($out);

            echo "文件生成有误！";

            exit;

            } else
        {

          $file = fopen($savepath.$filename,"r"); // 打开文件

           // 输入文件标签

            Header("Content-type: application/octet-stream");

           Header("Accept-Ranges: bytes");

           Header("Accept-Length: ".filesize($savepath.$filename));

            Header("Content-Disposition: attachment; filename=" . $filename);

           // 输出文件内容

            echo fread($file,filesize($savepath.$filename));

            fclose($file);

            exit;}


    }

}